# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=json-fortran
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=9.0.3
pkgrel=1
pkgdesc="A Modern Fortran JSON API. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64')
url='https://jacobwilliams.github.io/json-fortran/'
msys2_repository_url='https://github.com/jacobwilliams/json-fortran'
license=('spdx:BSD-3-Clause')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/jacobwilliams/json-fortran/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-install-dll.patch"
        "002-fix-pkgconfig-file.patch")
sha256sums=('ea6e02ab4873d43e2059d51d8392422d6f746a7a9ea0fb18ab28a3beb4fef13c'
            'a50c5a4db29c0436d8f9ec45451667cc110963c7d4e469df2dfa2ae020995b05'
            '926ff115c1310e5da71aee0bd08dfb3c0f619a8ecc70365394aac399b1bb816e')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.patch
  patch -Np1 -i "${srcdir}"/002-fix-pkgconfig-file.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=("-DENABLE_UNICODE=OFF")
  else
    _extra_config+=("-DENABLE_UNICODE=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DJSONFORTRAN_ENABLE_TESTS=OFF \
      -DUSE_GNU_INSTALL_CONVENTION=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
